<?php


namespace app\api\controller;


use app\api\BaseApi;
use app\service\TaskService;
use app\service\UserService;
use think\response\Json;

/**
 * Class Index
 * @package app\api\controller
 */
class Index extends BaseApi
{
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 可用余额和推广收益,下级团队人数,今日可做任务
     * json
     */
    public function indexUserData(): Json
    {
        $user_token = trim(input('user_token'));
        if (empty($user_token)) {
          return  $this->error('非法访问', '', $this::ERROR_CODE);
        }
        //检查用户是否在线
        $userService = new UserService();
        $userInfo = $userService->checkUerIsOnlineByToken($user_token);
        //dump($userInfo);die();
        if ($userInfo['status'] == 1 && !empty($userInfo['data'])) {
            //用户可用余额
            $balance = 0;
            //推广累计收益
            $totalPopularizeProfit = 0;
            //下级团队人数
            $teamUserNum = 0;
            //今日可做任务
            $todayTaskNum = 0;
            $user_id = $userInfo['data']['id'];
            $userAccount = $userService->getUserAccountInfoByUserID($user_id);
            if ($userAccount['status'] == 1 && !empty($userAccount['data'])) {
                $balance = $userAccount['data']['balance'];
                $totalPopularizeProfit = $userAccount['data']['total_popularize_profit'];
            }
            $teamUserNumInfo = $userService->getUserTeamUserNumByUserID($user_id);
            if ($teamUserNumInfo['status'] = 1) {
                $teamUserNum = $teamUserNumInfo['data'];
            }
            $user_vip_lv = $userInfo['data']['vip_lv'];
            $vipTaskNum = $this ->getVipTaskCount($user_vip_lv);
            $today = date("Ymd");
            //今天领取的任务数量
            $userTaskNum = $userService->getUserTaskNumByUserIDAndStatus($user_id, '', $today);
            if ($userTaskNum['status'] = 1) {
                //VIP等级可领取的任务数量减去当前已领取的任务数量，只按任务数量算，不管其他的
                $todayTaskNum =  $vipTaskNum - $userTaskNum['data'];
            }
            if($todayTaskNum<0){
                $todayTaskNum = 0;
            }
            $resultData = [
                'balance' => $balance,
                'totalPopularizeProfit' => $totalPopularizeProfit,
                'teamNum' => $teamUserNum,
                'todayTaskNum' => $todayTaskNum
            ];
            return $this->success('成功',$resultData);
        }else{
            return $this-> error($userInfo['msg'],'', $userInfo['data']);
        }
    }

    /**
     * 滚动页内容
     */
    public function scroll(): Json
    {
        $user_token = trim(input('user_token'));
        if (empty($user_token)) {
          return $this->error('非法访问', '', $this::ERROR_CODE);
        }
        //查出任务 top5
        //恭喜186******4657今日任务完成45单
        //检查用户是否在线
        $userService = new UserService();
        $userInfo = $userService->checkUerIsOnlineByToken($user_token);
        if ($userInfo['status'] == 0) {
          return $this->error($userInfo['msg'], '', $userInfo['data']);
        }
        $data = [];
        $taskService = new TaskService();
        $task_completion_time = strtotime("-60 day");
        $topFiveTask = $taskService->countUserTaskGroupAll($task_completion_time);
        if ($topFiveTask['status'] == 1 && !empty($topFiveTask['data'])) {
            foreach ($topFiveTask['data'] as $k => $value) {
                $mobile_txt = substr_replace($value['mobile'], '****', 3, 4);
                $data[] = '恭喜' . $mobile_txt . '今日任务完成' . $value['task_num'] . '单';
            }
        }
        //查出VIP top5
        //恭喜186******4657开通LV2成功
        $userService = new UserService();
        $vip_start_time = strtotime("-60 day");
        $newVipUerList = $userService->getNewVipUserTopFive($vip_start_time);
        if ($newVipUerList['status'] == 1 && !empty($newVipUerList['data'])) {
            foreach ($newVipUerList['data'] as $k => $value) {
                $mobile_txt = substr_replace($value['mobile'], '****', 3, 4);
                $data[] = '恭喜' . $mobile_txt . '开通LV' . $value['vip_lv'] . '成功';
            }
        }
        return $this->success('成功',$data);
    }

    /**
     * 首页推荐任务列表
     * @return Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function recommendTask(): Json
    {
        $user_token = trim(input('user_token'));
        if (empty($user_token)) {
            return $this->error('非法访问', '', $this::ERROR_CODE);
        }
        $page = trim(input('page'));
        $size = trim(input('size'));
        $task_lv = trim(input('lv'));
        $task_type = trim(input('task_type')); //任务类型:101 微视; 201 小红书; 301 快手; 401 抖音; 默认推荐全部类型的数据
        //查出任务 top5
        $userService = new UserService();
        $userInfo = $userService->checkUerIsOnlineByToken($user_token);
        if ($userInfo['status'] == 0) {
            return $this->error($userInfo['msg'], '', $userInfo['data']);
        }
        $taskService = new TaskService();
        $taskList = $taskService->getRecommendTask($page,$size,$task_lv,$userInfo['data']['id'],$task_type);
        if($taskList['status'] ==1){
            foreach ($taskList['data'] as $key => &$value){
                $task_type_name = '';
                switch ($value['task_type'])
                {
                    case '101':
                        $task_type_name = '微视';
                        break;
                    case '201':
                        $task_type_name = '小红书';
                        break;
                    case '301':
                        $task_type_name = '快手';
                        break;
                    case '401':
                        $task_type_name = '抖音';
                        break;
                    default:
                        $task_type_name = '未知';
                        break;
                }
                $value['task_type_name'] = $task_type_name;
            }
            return  $this->success($taskList['msg'], $taskList['data']);
        }else{
            return $this->error($taskList['msg'], '','100103-04-01');
        }
    }

    /**
     * 获取系统是否打开的标记
     * json
     */
    public function getSysIsOpen(): Json
    {
        $result = 1;//1 系统打开, 不等于1的就是关闭了
        return $this->success('成功',$result);
    }

}